package Bill;

import ConnnectData.DataConnect;
import entity.Bill;
import entity.ViewCart;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.swing.JOptionPane;


import javax.swing.UIManager;
import model.modelTable;

/**
 *
 * @author langtustupid
 */
public class View_Product_2 extends javax.swing.JInternalFrame {

    int a = 0;
    Connection Con;
    Statement Sta;
    ResultSet Res;
    private int staf_Id;
    public  ArrayList<ViewCart> listCart = new ArrayList<ViewCart>();

    //ViewCart view = new ViewCart();
    public View_Product_2() {
        try {
            initComponents();
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            Con = DataConnect.connectDB();
            Sta = Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public View_Product_2(int staffID) {
         try {
            initComponents();
            this.staf_Id = staffID;
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            Con = DataConnect.connectDB();
            Sta = Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

    public int getStaf_Id() {
        return staf_Id;
    }

    public void setStaf_Id(int staf_Id) {
        this.staf_Id = staf_Id;

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField_Search = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable_Product = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        lblProduct_Id = new javax.swing.JLabel();
        txtProduct_Id = new javax.swing.JTextField();
        lblProduct_Name = new javax.swing.JLabel();
        txtProduct_Name = new javax.swing.JTextField();
        lblquantity = new javax.swing.JLabel();
        txtQuantity = new javax.swing.JTextField();
        lblPrice = new javax.swing.JLabel();
        txtPrice = new javax.swing.JTextField();
        btnChonmua = new javax.swing.JButton();
        btnBill = new javax.swing.JButton();
        btnViewBill = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnShow = new javax.swing.JMenu();
        mnClose = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Product Management");
        setPreferredSize(new java.awt.Dimension(1056, 481));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 24)); // NOI18N
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/ProductMagament.png"))); // NOI18N

        jLabel2.setText("Product Name :");

        jTextField_Search.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField_SearchKeyReleased(evt);
            }
        });

        jTable_Product.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable_Product.setRowHeight(20);
        jTable_Product.setSelectionBackground(new java.awt.Color(255, 153, 255));
        jTable_Product.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable_ProductMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable_Product);

        lblProduct_Id.setText("Product_Id");

        lblProduct_Name.setText("Product_Name");

        lblquantity.setText("Quantity");

        lblPrice.setText("Price");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(lblProduct_Id)
                .addGap(18, 18, 18)
                .addComponent(txtProduct_Id, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addComponent(lblProduct_Name)
                .addGap(18, 18, 18)
                .addComponent(txtProduct_Name, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addComponent(lblquantity)
                .addGap(26, 26, 26)
                .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(lblPrice)
                .addGap(18, 18, 18)
                .addComponent(txtPrice, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                .addGap(93, 93, 93))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblProduct_Id)
                    .addComponent(txtProduct_Id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblProduct_Name)
                    .addComponent(txtProduct_Name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblquantity)
                    .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPrice)
                    .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        btnChonmua.setText("Chon mua");
        btnChonmua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChonmuaActionPerformed(evt);
            }
        });

        btnBill.setText("Bill");
        btnBill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBillActionPerformed(evt);
            }
        });

        btnViewBill.setText("View Bill");
        btnViewBill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewBillActionPerformed(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Search.jpg"))); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(341, 341, 341)
                .addComponent(jLabel2)
                .addGap(7, 7, 7)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(jTextField_Search, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(407, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(292, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(249, 249, 249))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1030, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(155, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(309, 309, 309)
                .addComponent(btnChonmua)
                .addGap(31, 31, 31)
                .addComponent(btnBill)
                .addGap(47, 47, 47)
                .addComponent(btnViewBill)
                .addContainerGap(458, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jLabel1)
                .addGap(9, 9, 9)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField_Search, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnViewBill)
                    .addComponent(btnBill)
                    .addComponent(btnChonmua))
                .addContainerGap(98, Short.MAX_VALUE))
        );

        jMenuBar1.setBackground(new java.awt.Color(255, 255, 255));

        mnShow.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/star.gif"))); // NOI18N
        mnShow.setText("Show Sales");
        mnShow.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnShowMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnShow);

        mnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/exit.png"))); // NOI18N
        mnClose.setText("Close");
        mnClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnCloseMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnClose);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField_SearchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_SearchKeyReleased
        try {
            Res = Sta.executeQuery("select Product.Product_Id,Product_Name,Product_Unit,Product_Price,Product_Quantity,Product_Description,convert(char(12),DateImport,101)as DateImport,Category_Name,Supplier_Name from((Product inner join Supplier on Product.Supplier_Id=Supplier.Supplier_Id)inner join Product_Category on Product_Category.Category_Id=Product.Category_Id)  where Product_status = 1 and Product_Name like '%" + jTextField_Search.getText().trim() + "%' order by Product_Name asc");
            jTable_Product.setModel(new modelTable(Res));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }//GEN-LAST:event_jTextField_SearchKeyReleased

    private void mnShowMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnShowMouseClicked
        try {
            String sql = "select Product_Id,Product_Name,Product_Unit,Product_Price,Product_Quantity,Product_Description "
                    + "from Product  where Product_Status = 1 order by Product_Id asc";
            Res = Sta.executeQuery(sql);

            jTable_Product.setModel(new modelTable(Res));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_mnShowMouseClicked

private void mnCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnCloseMouseClicked
// TODO add your handling code here:
    int Close = JOptionPane.showConfirmDialog(null, "Are you sure want close  !");
    if (Close == 0) {
        this.dispose();
    }
}//GEN-LAST:event_mnCloseMouseClicked

private void jTable_ProductMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable_ProductMouseClicked
// TODO add your handling code here:
    if (jTable_Product.getSelectedRow() != -1) {
        int i = jTable_Product.getSelectedRow();

        txtProduct_Id.setText(String.valueOf(jTable_Product.getValueAt(i, 0)));
        txtProduct_Name.setText(String.valueOf(jTable_Product.getValueAt(i, 1)));
        txtQuantity.setText(String.valueOf(jTable_Product.getValueAt(i, 4)));
        txtPrice.setText(String.valueOf(jTable_Product.getValueAt(i, 3)));
    }
}//GEN-LAST:event_jTable_ProductMouseClicked

private void btnChonmuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChonmuaActionPerformed
// TODO add your handling code here:
    try {

        float Price = Float.valueOf(txtPrice.getText());
        int Quantity = Integer.parseInt(txtQuantity.getText());
        ViewCart view = new ViewCart();
        view.setProduct_Id(Integer.valueOf(txtProduct_Id.getText()));
        view.setProduct_Name(txtProduct_Name.getText());
        view.setProduct_Quantity(Quantity);
        view.setProduct_Price(Price);
//        view.setTotal(Quantity * Price);
        listCart = Ma.ManagerViewCart.addViewCart(view);
        JOptionPane.showMessageDialog(this, "Da them sp");
    } catch (Exception e) {
        e.printStackTrace();
    }
}//GEN-LAST:event_btnChonmuaActionPerformed

private void btnBillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBillActionPerformed
// TODO add your handling code here:

//    ViewCart view = new ViewCart();
//    ArrayList<ViewCart> view1 = new ArrayList<ViewCart>();
//    view1 = Ma.ManagerViewCart.addViewCart(view);
    ViewCart3 viewC1 = new ViewCart3(listCart);
    
    viewC1.setStaf_id1(staf_Id);
   // View_Bill view_bill = new View_Bill(listCart);
    
    //viewC1.showList();
    viewC1.setVisible(true);
}//GEN-LAST:event_btnBillActionPerformed

private void btnViewBillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewBillActionPerformed
// TODO add your handling code here:
    View_Bill view_bill = new View_Bill(listCart,staf_Id);
    //view_bill.setStaf_id(staf_Id);
    view_bill.setVisible(true);
}//GEN-LAST:event_btnViewBillActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new View_Product_2().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBill;
    private javax.swing.JButton btnChonmua;
    private javax.swing.JButton btnViewBill;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable_Product;
    private javax.swing.JTextField jTextField_Search;
    private javax.swing.JLabel lblPrice;
    private javax.swing.JLabel lblProduct_Id;
    private javax.swing.JLabel lblProduct_Name;
    private javax.swing.JLabel lblquantity;
    private javax.swing.JMenu mnClose;
    private javax.swing.JMenu mnShow;
    private javax.swing.JTextField txtPrice;
    private javax.swing.JTextField txtProduct_Id;
    private javax.swing.JTextField txtProduct_Name;
    private javax.swing.JTextField txtQuantity;
    // End of variables declaration//GEN-END:variables
}
